#!/usr/bin/env python

import nltk
from nltk.corpus import brown
from nltk import ConditionalFreqDist
brown_tagged_sents = brown.tagged_sents(categories='news')

cfd = ConditionalFreqDist(
	((y[1], z[0]), z[1])
	for sent in brown_tagged_sents
	for y, z in nltk.bigrams(sent)
	)
ambiguous_contexts = [ c for c in cfd.conditions() if len(cfd[c]) > 1 ]
print (float)(sum(cfd[c].N() for c in ambiguous_contexts)) / cfd.N()